package com.zrrd.javaSEPlus._04_reflect;

import java.lang.reflect.Constructor;

/**
 * @author WanJl
 * @version 1.0
 * @title ReflectConstructor
 * @description
 * @create 2023/11/12 11:46
 */
public class ReflectConstructor {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        //method01();
        //method02();
        //method03();
        method04();
    }

    /**
     * Constructor<?>[] getConstructors()返回所有公共构造方法对象的数组
     * @throws ClassNotFoundException
     */
    public static void method01() throws ClassNotFoundException {
        //1.获取Class对象
        Class<?> clazz = Class.forName("com.zrrd.javaSEPlus._04_reflect.Student");
        Constructor<?>[] constructors = clazz.getConstructors();
        /*
        for(元素的类型 变量名:数组或集合对象){
            直接调用变量名，就可以获取每一个元素。
        }
         */
        for (Constructor c :constructors) {//迭代
            System.out.println(c);
        }
    }

    /**
     * Constructor<?>[] getDeclaredConstructors() 返回所有构造方法对象的数组
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     */
    public static void method02() throws ClassNotFoundException, NoSuchMethodException {
        //1.获取Class对象
        Class<?> clazz = Class.forName("com.zrrd.javaSEPlus._04_reflect.Student");
        Constructor<?>[] constructors = clazz.getDeclaredConstructors();

        for (Constructor c :constructors) {//迭代
            System.out.println(c);
        }
    }

    /**
     * Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)返回单个构造方法对象
     * 无论这个构造方法是否是private修饰的。
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     */
    public static void method03() throws ClassNotFoundException, NoSuchMethodException {
        //1.获取Class对象
        Class<?> clazz = Class.forName("com.zrrd.javaSEPlus._04_reflect.Student");
        //getDeclaredConstructor()方法内，不传入参数，就是无参的。
        Constructor<?> c = clazz.getDeclaredConstructor();

        Constructor<?> c1 = clazz.getDeclaredConstructor(String.class);
        Constructor<?> c2 = clazz.getDeclaredConstructor(String.class,int.class);
        System.out.println(c);
        System.out.println(c1);
        System.out.println(c2);
    }

    /**
     * Constructor<T> getConstructor(Class<?>... parameterTypes)返回单个公共构造方法对象
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     */
    public static void method04() throws ClassNotFoundException, NoSuchMethodException {
        //1.获取Class对象
        Class<?> clazz = Class.forName("com.zrrd.javaSEPlus._04_reflect.Student");
        Constructor<?> c1 = clazz.getConstructor();
        Constructor<?> c2 = clazz.getConstructor(String.class);
        Constructor<?> c3 = clazz.getConstructor(String.class,int.class);

        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
    }
}
